package com.controller;

import com.util.BaseDao;
import javafx.fxml.FXML;
import javafx.scene.text.Text;

import java.sql.*;

import static com.Main.changeUI;

public class CountTotal {
    @FXML
    private Text prisonerNumber;

    @FXML
    private Text visitorNumber;

    @FXML
    private Text workerNumber;

    @FXML
    private Text managerNumber;

    public void initialize(){

        prisonerCount();

        workerCount();

        VisitorCount();

        managerCount();

    }
    public void prisonerCount(){
        // 假设您已经有了一个数据库连接方法BaseDao.getConn()
        String sql = "USE PManage;" +
                "SELECT COUNT(*) AS TotalPrisoners " +
                "FROM prisoner_Pwork;";

        try (Connection connection = BaseDao.getConn();
             Statement statement = connection.createStatement();
             ResultSet resultSet = statement.executeQuery(sql)) {

            if (resultSet.next()) { // 移动到结果集的下一行
                int totalPrisoners = resultSet.getInt("TotalPrisoners"); // 获取统计值
                updatePrisonerNumberText(totalPrisoners); // 更新Text组件的文本
            }
        } catch (SQLException e) {
            e.printStackTrace();
            // 处理异常，例如显示错误信息
        }
    }
    public void updatePrisonerNumberText(int totalPrisoners) {
        prisonerNumber.setText(String.valueOf(totalPrisoners)); // 将统计结果转换为字符串并设置为Text的文本
    }


    public void workerCount(){
        String sql = "USE PManage;" +
                "SELECT COUNT(*) AS TotalWorker " +
                "FROM worker;";

        try (Connection connection = BaseDao.getConn();
             PreparedStatement preparedStatement = connection.prepareStatement(sql);
             ResultSet resultSet = preparedStatement.executeQuery()) {

            if (resultSet.next()) { // 检查是否有结果返回
                int totalWorker = resultSet.getInt("TotalWorker"); // 获取统计的工人数
                updateTextWithWorkerCount(totalWorker); // 更新Text组件显示的文本
            }
        } catch (SQLException e) {
            e.printStackTrace();
            // 这里可以添加错误处理逻辑，例如弹出错误提示框
        }


    }

    private void updateTextWithWorkerCount(int totalWorker) {
        workerNumber.setText(String.valueOf(totalWorker)); // 设置Text组件的文本
    }

    public void VisitorCount(){
        String sql = "USE PManage;" +
                "SELECT COUNT(*) AS TotalVisitor FROM visitor;";

        try (Connection connection = BaseDao.getConn();
             PreparedStatement preparedStatement = connection.prepareStatement(sql);
             ResultSet resultSet = preparedStatement.executeQuery()) {

            if (resultSet.next()) { // 检查是否有结果返回
                int totalVisitor = resultSet.getInt("TotalVisitor"); // 获取统计的访问者总数
                updateTextWithVisitorCount(totalVisitor); // 更新Text组件显示的文本
            }
        } catch (SQLException e) {
            e.printStackTrace();
            // 这里可以添加错误处理逻辑，例如弹出错误提示框
        }

    }
    private void updateTextWithVisitorCount(int totalVisitor) {
        visitorNumber.setText(String.valueOf(totalVisitor)); // 设置Text组件的文本
    }

    public void managerCount(){
        String sql = "USE PManage;" +
                "SELECT COUNT(*) AS TotalWorker FROM worker " +
                "WHERE jobtype LIKE '管理员';";

        try (Connection connection = BaseDao.getConn();
             PreparedStatement preparedStatement = connection.prepareStatement(sql);
             ResultSet resultSet = preparedStatement.executeQuery()) {

            if (resultSet.next()) { // 检查是否有结果返回
                int totalWorker = resultSet.getInt("TotalWorker"); // 获取统计的管理员工人数
                updateTextWithManagerCount(totalWorker); // 更新Text组件显示的文本
            }
        } catch (SQLException e) {
            e.printStackTrace();
            // 这里可以添加错误处理逻辑，例如弹出错误提示框
        }


    }

    private void updateTextWithManagerCount(int totalWorker) {
        managerNumber.setText(String.valueOf(totalWorker));
    }

    @FXML
    void doquit() {
        changeUI("ui/MainInterFace.fxml");
    }
}
